<?php
require_once 'Banco/InfoBanco.php';
if (!empty($_POST)) {
    $request = md5( implode( $_POST ) );  
    if(isset($_POST['edtChat'])){
        if( isset( $_SESSION['last_request'] ) && $_SESSION['last_request']== $request ){  
                //Refresh
        }else{  
                $_SESSION['last_request']  = $request;  
                CadastraMensagemChat($_SESSION['id'],$_POST['edtChat']);  
        }  
    }
}

function CadastraMensagemChat($cod, $mensagem){
        $conect = getConexao();
        $ok = mysql_select_db("radio463_radio", $conect);
        mysql_query("INSERT INTO chat VALUES (default, ". $cod ." , '" .  strip_tags(mysql_real_escape_string(utf8_encode($mensagem))) . "',NOW(), default)");
        echo  mysql_error();
}

function getMensagensChat() {
    $conect = getConexao();
    $ok = mysql_select_db("radio463_radio", $conect);
    $consulta = "SELECT * FROM chat WHERE oculto=0 OR oculto=2 ORDER BY data DESC LIMIT 0 , 10";
    $rs = mysql_query($consulta);
    while ($row = mysql_fetch_array($rs)) {
        list( $date, $time ) = explode(' ', $row['data']);
        ?>
        <blockquote>
            <p><?php echo utf8_decode($row['mensagem'])?></p>
            <small><?php echo getOuvinte($row['cod_ouvinte'])." ".$date." ".$time;?></small>
        </blockquote>
        <?php
    }
}

    function getOuvinte($id) {
        $conect = getConexao();
        $ok = mysql_select_db("radio463_radio", $conect);
        $consulta = "SELECT * FROM ouvinte WHERE id_Ouvinte=" . $id;
        $rs = mysql_query($consulta);
        if ($row = mysql_fetch_array($rs)) {
            return ucwords(strtolower($row['nome_completo']));
        }
        return "N/A";
    }
?>
